/* TextTree.cpp
Michael Zahniser, 2008

Function definitions for the TextTree class.
*/

#include "TextTree.h"



TextTree::TextTree()
	: previousIndent(0x7FFFFFFF)
{
}



void TextTree::Clear()
{
    lines.clear();
    previousIndent = 0x7FFFFFFF;
}



void TextTree::Add(int indent, const string &text)
{
	// If this is a child of the previous line, make the previous line expandable.
	if(indent > previousIndent)
	{
		list<Line>::iterator previous = lines.end();
		--previous;
		previous->SetMode('-');
	}

	lines.push_back(Line(indent, text));
	previousIndent = indent;
}



TextTree::Iterator::Iterator(TextTree &tree)
	: it(tree.lines.begin()), end(tree.lines.end())
{
}



void TextTree::Iterator::operator++()
{
	if(it == end)
		return;
	
	int indent = (it->Mode() == '+') ? it->Indent() : 0x7FFFFFFF;
	while(++it != end)
		if(it->Indent() <= indent)
			return;
}



void TextTree::Iterator::operator++(int)
{
	++*this;
}



TextTree::Iterator::operator bool() const
{
	return !!*this;
}



bool TextTree::Iterator::operator!() const
{
	return (it == end);
}



const string &TextTree::Iterator::operator*() const
{
	return it->ToString();
}



const string *TextTree::Iterator::operator->() const
{
	return &**this;
}


		
void TextTree::Iterator::Expand()
{
	if(it->Mode() == '+')
		it->SetMode('-');
}



void TextTree::Iterator::Collapse()
{
	if(it->Mode() == '-')
		it->SetMode('+');
}
